/*
 *  Copyright 2009 David Kuhn <david.s.kuhn at gmail.com>.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package com.google.code.teamtools.client.model;

import com.extjs.gxt.ui.client.data.BaseModelData;
import com.google.gwt.user.client.rpc.IsSerializable;

/**
 * Base model for all resources.
 * 
 * @author David Kuhn <david.s.kuhn at gmail.com>
 */
public abstract class ResourceModel extends BaseModelData implements IsSerializable
{
    private static final long serialVersionUID = 1L;

    protected ResourceModel()
    {
        super();
        setId(null);
    }

    protected ResourceModel(Long id)
    {
        this();
        setId(id);
    }

    protected ResourceModel(String name)
    {
        this();
        setName(name);
    }

    protected ResourceModel(String name, IResourceOwner owner)
    {
        this(name);
        setOwner(owner);
    }

    public IResourceOwner getOwner()
    {
        return get("owner");
    }

    public void setOwner(IResourceOwner owner)
    {
        set("owner", owner);
    }

    public Long getId()
    {
        return get("id");
    }

    public String getName()
    {
        return get("name");
    }

    public void setName(String name)
    {
        set("name", name);
    }
    
    @Override
    public boolean equals(Object obj)
    {
        if (obj != null && obj instanceof ResourceModel) {
            ResourceModel mobj = (ResourceModel) obj;
            return (getId().equals(mobj.getId()) && 
                    getName().equals(mobj.getName()));
        }
        return super.equals(obj);
    }

    @Override
    public String toString()
    {
        StringBuilder sb = new StringBuilder("ResourceModel[");
        sb.append("id:" + getId());
        sb.append("|name:" + getName());
        sb.append("|owner:" + String.valueOf(getOwner()));
        sb.append("]");
        return sb.toString();
    }
    
    protected void setId(Long id)
    {
        set("id", id);
    }
}
